package com.agent.service.impl;

import com.agent.entity.CustomKPI;
import com.agent.entity.Report;
import com.agent.repository.ReportMapper;
import com.agent.service.IReportService;
import com.agent.util.Constants;
import com.agent.vo.CustomKPIVO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements IReportService {
    @Autowired
    ReportMapper reportMapper;



    @Override
    public List<Report> searchSourceReport(Map<String, String> paramMap)throws Exception {
        List<Report> result = new ArrayList<>();
        concatTime(paramMap);
        List<Report> reportList = reportMapper.searchSourceReport(paramMap);
        Map<String, List<Report>> collect = reportList.parallelStream().collect(Collectors.groupingBy(Report::getSource));
        collect.forEach((source,reports) ->{
            Report report = new Report();
            report.setSource(Constants.getSourceMap(source));
            Integer totalCount = reports.stream().collect(Collectors.summingInt(Report::getCount));
            report.setTotalCount(totalCount);// 总数
            report.setAvgCount(totalCount.doubleValue()/reports.size() +"");// 日平均数量
            result.add(report);
        });
        return result;
    }

    @Override
    public List<Report> searchDeptReport(Integer page, Integer rows,Map<String, String> paramMap)throws Exception {
        List<Report> result = new ArrayList<>();
        concatTime(paramMap);
        PageHelper.startPage(page, rows,true, true, true);
        List<Report> reportList = reportMapper.searchDeptReport(paramMap);
        Map<String, List<Report>> collect = reportList.parallelStream().collect(Collectors.groupingBy(Report::getDeptName));
        collect.forEach((deptName,reports) ->{
            Report report = new Report();
            report.setDeptName(deptName); //部门名称
            Integer totalCount = reports.stream().collect(Collectors.summingInt(Report::getCount));
            report.setTotalCount(totalCount);// 总数
            report.setAvgCount(String.format("%.2f",totalCount.doubleValue()/reports.size()));// 日平均数量
            result.add(report);
        });
        return result;
    }
    // 1.工程师（包含坐席人员，因为部分坐席点击的完成） 2.数量 3.平均响应时间  分 秒 4.平均服务时间  分 秒 5.平均服务质量 分数（0.0-5.0）
    // -- 6.平均服务态度分数（0.0 -5.0）
    @Override
    public List<CustomKPIVO> searchCustomKPI(Integer page, Integer rows, Map<String, String> paramMap) throws Exception {
        List<CustomKPIVO> result = new ArrayList<>();
        concatTime(paramMap);
//        PageHelper.startPage(page, rows,true, true, true);
        List<CustomKPI> list = reportMapper.searchCustomKPI(paramMap);
        Map<String, List<CustomKPI>> map = list.parallelStream().collect(Collectors.groupingBy(CustomKPI::getCurrentId));
        map.forEach((currentId,kpiList) ->{
            CustomKPIVO resultVO = new CustomKPIVO();
            resultVO.setCurrentId(currentId);
            int size = kpiList.size();
            if(size !=0){
                long[] time = new long[4];
                CustomKPI temp = kpiList.get(0);
                resultVO.setCount(temp.getCount());
                resultVO.setCurrentName(temp.getCurrentName());
                kpiList.parallelStream().forEach(customKPI -> {
                    time[0] += customKPI.getBeginProcessTime().getTime() - customKPI.getCreateTime().getTime();// 响应时间
                    time[1] += customKPI.getEndProcessTime().getTime() - customKPI.getBeginProcessTime().getTime();// 服务时间
                    time[2] += Long.parseLong(customKPI.getQualityOfService()); // 服务质量
                    time[3] += Long.parseLong(customKPI.getServiceAttitude()); // 服务态度
                });
                resultVO.setResponseTime(parseTime(time[0]/size));
                resultVO.setServerTime(parseTime(time[1]/size));
                resultVO.setQualityOfService(String.format("%.2f",((Long) time[2]).doubleValue() / size));
                resultVO.setServiceAttitude(String.format("%.2f",((Long) time[3]).doubleValue() / size));
                result.add(resultVO);
            }
        });
        return result;
    }
    // 转换为 分 秒
    private String parseTime(long time){
        long minute = time / 1000 / 60;
        long secone = time / 1000 % 60;
        return minute+"分"+secone+"秒";
    }
    private void concatTime(Map<String, String> paramMap){
        if(paramMap.containsKey("beingTime")){
            paramMap.put("beingTime",paramMap.get("beingTime").concat(" 00:00:00"));
        }
        if (paramMap.containsKey("endTime")) {
            paramMap.put("endTime",paramMap.get("endTime").concat(" 23:59:59"));
        }
    }
}
